package com.jtjrenren.android.taxi.driver.core.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jtjrenren.android.taxi.driver.Constants;
import com.jtjrenren.android.taxi.driver.base.BaseRcyAdapter;
import com.jtjrenren.android.taxi.driver.bean.api.ApiOrderList;
import com.jtjrenren.android.taxi.driver.R;
import com.jtjrenren.android.taxi.driver.utils.ModuleUtils;
import com.library.utils.StringUtils;

/**
 * Created by Administrator on 2016/3/11.
 */
public class OrderHistoryAdapter extends BaseRcyAdapter<ApiOrderList.OrderListEntity> {
    public interface ItemClickListener {
        void itemClick(int position);
        void  callPhone(String phone);
    }

    private ItemClickListener itemClickListener;

    public OrderHistoryAdapter(Context context) {
        super(context);
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_order_history, parent, false);

        return new OrderItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        OrderItemViewHolder itemViewHolder = (OrderItemViewHolder) holder;

        ApiOrderList.OrderListEntity info = list.get(position);
        itemViewHolder.tv_start.setText(info.getStartAddr());
        itemViewHolder.tv_end.setText(info.getEndAddr());
        itemViewHolder.tv_cost.setText(info.getPayTotal() + "");
        itemViewHolder.tv_date.setText(info.getCreateDate());

        int status = ModuleUtils.orderStatusAdapter(StringUtils.isEmpty(info.getStatus()) ? 0 : Integer.parseInt(info.getStatus()));
        String stausDesc = "未完成";
        if (status == Constants.STATUS_ORDER_CANCLE) {
            stausDesc = "取消";
        } else if (status == Constants.STATUS_ORDER_FINISH) {
            stausDesc = "完成";
            int i = ModuleUtils.orderPayStatusAdapter(info.getPayStatus());
            if (i==Constants.STATUS_ORDER_PAY_NOTCOMMIT){
                stausDesc = "未提交付款";
            }else  if (i==Constants.STATUS_ORDER_PAY_YES){

            }else
                stausDesc = "未支付";

        }
        itemViewHolder.tv_status.setText(stausDesc);
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public class OrderItemViewHolder extends RecyclerView.ViewHolder {
        TextView tv_start, tv_end, tv_cost, tv_status, tv_date;
        ImageButton ib_hjck;

        LinearLayout ll_con;

        public OrderItemViewHolder(View itemView) {
            super(itemView);
            initViews();
        }

        private void initViews() {
            tv_start = (TextView) itemView.findViewById(R.id.item_history_start);
            tv_end = (TextView) itemView.findViewById(R.id.item_history_end);
            tv_cost = (TextView) itemView.findViewById(R.id.item_history_cf);
            tv_status = (TextView) itemView.findViewById(R.id.item_history_status);
            tv_date = (TextView) itemView.findViewById(R.id.item_history_date);
            ib_hjck = (ImageButton) itemView.findViewById(R.id.item_history_hjck);


            ll_con = (LinearLayout) itemView.findViewById(R.id.item_history_con);


            ib_hjck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                   if (itemClickListener!=null)
                       itemClickListener.callPhone(list.get(getAdapterPosition()).getMobilePhone());
                }
            });

            ll_con.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null)
                        itemClickListener.itemClick(getAdapterPosition());
                }
            });
        }
    }
}
